package com.dongjiantong.common.redis;


import com.dongjiantong.common.redis.client.RedisKeys;
import com.dongjiantong.common.redis.client.RedisStrings;
import com.google.gson.reflect.TypeToken;
import redis.clients.jedis.Jedis;

import java.io.Serializable;
import java.util.List;
import java.util.Map;


/**
 *
 *
 * @author muzhong
 * @create 2017-10-30
 */
public class RedisUtil {

    /**缓存生存时间 */
    private  static final  int expire = 60000;


    /**
     * 设置过期时间
     * @param key
     * @param seconds
     */
    public static void expire(String key, int seconds) {
        if (seconds <= 0) {
            return;
        }
        Jedis jedis = JedisUtil.getJedis();
        jedis.expire(key, seconds);
        JedisUtil.releaseResource(jedis);
    }

    /**
     * 设置默认过期时间
     * @author ruan 2013-4-11
     * @param key
     */
    public static  void expire(String key) {
        expire(key, expire);
    }


    /**
     * 将对象存放到redis中
     * @param key
     * @param obj
     * @throws Exception
     */
    public static void setObject(String key,Object obj){
        setObject(key,obj,0);
    }

    public static void setObject(String key,Object obj,int timeout){

        RedisStrings strings= new RedisStrings();
        try {
            if (obj != null) {
//                byte[] bytes = ObjectUtil.jsonSerialize(obj);
//
//                strings.set(key.getBytes(), bytes);

               String json =  ObjectUtil.objectToJsonStr(obj);
                strings.set(key, json);

                if (timeout > 0) {
                    expire(key, timeout);
                }
            }
        }catch (Exception e) {
            e.printStackTrace();

        }
    }
    /**
     * 获取redis里面对象
     * @param key
     * @param
     * @return
     * @throws Exception
     */
    public static <T extends Serializable> T getObject(String key,Class<T> type){
        RedisStrings strings= new RedisStrings();
        try {
//            byte[] bytes = strings.get(key.getBytes());
//            @SuppressWarnings("unchecked")
//            T object = (T) ObjectUtil.jsonDeserialize(bytes);

            String json = strings.get(key);
            @SuppressWarnings("unchecked")
            T object = (T) ObjectUtil.jsonToObject(json,type);
            return object;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static <T extends Serializable> T getMaps(String key)
    {
        RedisStrings strings= new RedisStrings();
        try {

            String json = strings.get(key);
            @SuppressWarnings("unchecked")
            T object = (T) ObjectUtil.jsonToMap(json);
            return object;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void setEX(String key,Object obj,int timeout)
    {
        RedisStrings strings= new RedisStrings();

        try {
            if (obj != null) {
               // byte[] bytes = ObjectUtil.jsonSerialize(obj);
                String json =  ObjectUtil.objectToJsonStr(obj);
                strings.setEx(key, timeout,json);

            }
        }catch (Exception e) {
            e.printStackTrace();

        }
    }


    /**
     * 设置list
     * @param <T>
     * @throws Exception
     */
    public static <T> void setList(String key,List<T> list){
        setList(key,list,0);
    }
    /**
     * 设置list
     * @param <T>
     * @throws Exception
     */
    public  static <T> void setList(String key,List<T> list,int timeout){
        RedisStrings strings= new RedisStrings();
        try {
            strings.set(key, ObjectUtil.objectToJsonStr(list));

            if (timeout > 0) {
                expire(key, timeout);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    /**
     * 获取
     * @param key
     * @return
     */
    public static <T> List<T> getList(String key){

        RedisStrings strings= new RedisStrings();
        RedisKeys keys = new RedisKeys();
        if(!keys.exists(key))
        {
            return null;
        }
        try {

            List<T> dataList = ObjectUtil.jsonToList(strings.get(key));

           // List<T> dataList = ObjectUtil.jsonDeserialize(strings.get(key.getBytes()));

            return dataList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

}
